﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;

namespace CS544_Protocol_Client.Forms
{
    public partial class ChatRoomForm : Form
    {

        private ClientControl myClientControl;
        public string RoomName { get; private set; }
        public event EventHandler SelectedUser;

        public ChatRoomForm(ClientControl clientControl,string roomName)
        {
            InitializeComponent();
            myClientControl = new ClientControl();
            myClientControl = clientControl;
            this.RoomName = roomName;
            myClientControl.NewChatRoomMessage += new EventHandler(myClientControl_NewChatRoomMessage);
            this.OnlineUserListBox.SelectedIndexChanged += new EventHandler(OnlineUserListBox_SelectedIndexChanged);
            this.myClientControl.UserListUpdate += new EventHandler(myClientControl_UserListUpdate);
            this.myClientControl.UserAway += new ClientEventHandler(myClientControl_UserAway);
            this.myClientControl.UserOnline += new ClientEventHandler(myClientControl_UserOnline);
            this.myClientControl.StatusResult += new ClientEventHandler(myClientControl_StatusResult);
        }

        void myClientControl_StatusResult(object sender, ClientEventArgs e)
        {
            Invoke((MethodInvoker)updateUserList);
        }


        //handles when user sets status to online
        void myClientControl_UserOnline(object sender, ClientEventArgs e)
        {
            Invoke((MethodInvoker)updateUserList);
        }


        //Handles when user sets status as away
        void myClientControl_UserAway(object sender, ClientEventArgs e)
        {
            Invoke((MethodInvoker)updateUserList);        
        }

    

        //Handle when client receives a new room message
        void myClientControl_NewChatRoomMessage(object sender, EventArgs e)
        {
            Invoke((MethodInvoker)SafeChatTextBoxUpdate);
        }

        void SafeChatTextBoxUpdate()
        {
            this.ChatTextBox.AppendText(myClientControl.getNewRoomMessage() + Environment.NewLine);
        }


        //Handle when the user list is updated from the server
        void myClientControl_UserListUpdate(object sender, EventArgs e)
        {
            Invoke((MethodInvoker)updateUserList);            
        }


        //Handles selected changed event for user list box
        void OnlineUserListBox_SelectedIndexChanged(object sender, EventArgs e)
        {            
                       
            OnSelectedUser();        
        }


        public string getSelectedUser()
        {
            string selectedUser = this.OnlineUserListBox.SelectedItem.ToString();

            return selectedUser;
        }
         


        //Handle click event for send button
        private void SendButton_Click(object sender, EventArgs e)
        {

            myClientControl.sendRoomMessage(this.EnterMessageTextBox.Text);            
            this.EnterMessageTextBox.Clear();
        }


        //update user list
        public void updateUserList()
        {

            try
            {

                List<User> userList = myClientControl.getUserListForDisplay();
                List<string> users = new List<string>();
                this.OnlineUserListBox.Items.Clear();
                foreach (User user in userList)
                {
                    this.OnlineUserListBox.Items.Add(user.Username + " " + user.Status);
                }


            
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error loading user list " + ex.ToString());
            }


        }

        

        private void OnSelectedUser()
        {
            if (SelectedUser != null)
                SelectedUser(this, new EventArgs());
        }

   




        

    }
}
